package com.yoda.mall.mysql.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.api.HomeRecommendProductApi;
import com.yoda.mall.model.entity.sms.HomeNewProduct;
import com.yoda.mall.model.entity.sms.HomeRecommendProduct;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.mysql.mapper.HomeRecommendProductMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author shawn
 * @date 2022年 12月 17日 10:40
 */
@DubboService
public class HomeRecommendProductApiImpl implements HomeRecommendProductApi {

    @Resource
    private HomeRecommendProductMapper homeRecommendProductMapper;

    @Override
    public PageResult<HomeRecommendProduct> getHomeRecommendProductPage(Integer page, Integer pageSize, String productName, Integer recommendStatus) {
        Page<HomeRecommendProduct> productPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<HomeRecommendProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(productName), HomeRecommendProduct::getProductName,productName);
        wrapper.eq(Objects.nonNull(recommendStatus), HomeRecommendProduct::getRecommendStatus,recommendStatus);
        wrapper.orderByDesc(HomeRecommendProduct::getSort);
        homeRecommendProductMapper.selectPage(productPage, wrapper);
        return new PageResult<>(page, pageSize, (int) productPage.getTotal(), productPage.getRecords());
    }

    @Override
    public void changeRecommendProductSort(Long id, Integer sort) {
        HomeRecommendProduct recommendProduct = new HomeRecommendProduct();
        recommendProduct.setId(id);
        recommendProduct.setSort(sort);
        homeRecommendProductMapper.updateById(recommendProduct);
    }

    @Override
    public void updateRecommendProductStatus(List<Long> ids, Integer recommendStatus) {
        homeRecommendProductMapper.updateRecommendProductStatus(ids,recommendStatus);
    }

    @Override
    public void deleteRecommendProductByIds(List<Long> ids) {
        homeRecommendProductMapper.deleteBatchIds(ids);
    }

    @Override
    public Integer createRecommendProduct(List<HomeRecommendProduct> homeRecommendProducts) {
       return homeRecommendProductMapper.saveBatch(homeRecommendProducts);
    }
}
